package net.wsfcp.android.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import net.wsfcp.android.http.mvp.IView;
import net.wsfcp.android.utils.DpUtils;
import net.wsfcp.android.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;

/**
 * Created by Shaoboyang on 2018/12/14.
 */
//基类fragment
public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements
        IView, View.OnClickListener {
    protected View view;

    protected P mPresenter;
    private ProgressDialog pg;
    Toast toast=null;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                otherViewClick(v);
                break;
        }
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = getView(inflater, container);
        mPresenter = loadPresenter();
        ButterKnife.bind(this, view);
        if(!EventBus.getDefault().isRegistered(this)){//加上判断
            EventBus.getDefault().register(this);
        }

        pg=new ProgressDialog(getContext());//等待框
        initCommonData();
        initView();
        initListener();
        initData();

        return view;
    }

    public void showPg() {
        pg.show();
    }

    public void dismissPg() {
        pg.dismiss();
    }

    protected abstract P loadPresenter();

    private void initCommonData() {

        if (mPresenter != null)
            mPresenter.attachView(this);
    }

    protected abstract void initData();

    protected abstract void initListener();

    protected abstract void initView();

    protected abstract int getLayoutId();

    protected abstract void otherViewClick(View view);

    /**
     * @return 显示的内容
     */
    public View getView(LayoutInflater inflater, ViewGroup container) {

        View view = inflater.inflate(getLayoutId(), container, false);
        return view;
    }


    /**
     * @param str 显示一个内容为str的toast
     */
    public void toast(String str) {
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();

//        if (toast == null) {
//            toast = Toast.makeText(getContext(), str, Toast.LENGTH_SHORT);
//        } else {
//            toast.setText(str);
//            toast.setDuration(Toast.LENGTH_SHORT);
//        }
//        toast.show();
    }

    /**
     * @param contentId 显示一个内容为contentId指定的toast
     */
    public void toast(int contentId) {
        Toast.makeText(getContext(), contentId, Toast.LENGTH_SHORT).show();
    }


    /**
     * @param str 日志的处理
     */
    public void LogE(String str) {
        LogUtils.e(getClass(), str);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();

        if (EventBus.getDefault().isRegistered(this))//加上判断
            EventBus.getDefault().unregister(this);
    }


    protected void startActivity(Class activity) {
        Intent intent = new Intent(getContext(), activity);
        startActivity(intent);
    }


    public int dp(float dpValue) {
        return DpUtils.dp2px(dpValue);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Object event) {
    }
}
